package com.chinawayltd.ews.sample.taskDispatch.pojo;

import com.alibaba.fastjson.JSONObject;
import com.chinawayltd.ews.sample.taskDispatch.TaskDispatchConstants;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: greeProject.timingTaskCenter
 * @description:
 * @author: xiazj
 * @create: 2018-07-15 22:02
 **/
public class BaseTask {

    private List<JSONObject> orderList = new ArrayList<>();
    private JSONObject vehicle;
    private double currentPallet;
    private double actualPallet;
    private Integer maxPallet;
    private Double minStarttime;


    public BaseTask(JSONObject order, JSONObject vehicle) {
        maxPallet = vehicle.getInteger(TaskDispatchConstants.VEHICLE_VOLUME);
        this.vehicle = vehicle;

        minStarttime = order.getDouble(TaskDispatchConstants.TASKLIST_STARTTIME);
        this.addOrder(order);
    }

    public void addOrder(JSONObject order){
        int samePallet = 0;
        //处理订单拼版标识，拼版规则： 同一装货点、卸货点、最早送货时间、最晚送货时间
        JSONObject lastOrder = getLastOrder();
        if(lastOrder != null){
            samePallet = lastOrder.getInteger(TaskDispatchConstants.TASKLIST_SAME_PALLET);
            if(!orderFieldContrast(lastOrder,order, TaskDispatchConstants.TASKLIST_PICKUP)
                    || !orderFieldContrast(lastOrder,order, TaskDispatchConstants.TASKLIST_DELIVERY)
                    || !orderFieldContrast(lastOrder,order, TaskDispatchConstants.TASKLIST_STARTTIME)
                    || !orderFieldContrast(lastOrder,order, TaskDispatchConstants.TASKLIST_ENDTIME)){
                samePallet++;
            }
        }

        order.put(TaskDispatchConstants.TASKLIST_SAME_PALLET, samePallet);
        this.orderList.add(order);
        //统计当前任务的总板位数
        countCurrentPallet();
    }

    private void countCurrentPallet(){
        double totalPallet = 0;
        double tempActualPallet = 0;
        double tempPallet = 0;

        Integer lastSamePallet = 0;

        for (JSONObject order : orderList) {
            Double orderPallet = order.getDouble(TaskDispatchConstants.TASKLIST_PALLET);
            Integer samePallet = order.getInteger(TaskDispatchConstants.TASKLIST_SAME_PALLET);

            tempActualPallet += orderPallet;

            //如果是拼版的订单，就把同一拼版标识的订单总和向上取整
            if(lastSamePallet == samePallet){
                tempPallet += orderPallet;
            }else{
                totalPallet += Math.ceil(tempPallet);
                tempPallet = orderPallet;
                lastSamePallet = samePallet;
            }
        }
        totalPallet += Math.ceil(tempPallet);
        currentPallet = totalPallet;
        actualPallet = tempActualPallet;
    }

    private boolean orderFieldContrast(JSONObject order1, JSONObject order2, String fieldName){
        String fieldVal1 = order1.getString(fieldName);
        String fieldVal2 = order2.getString(fieldName);
        if(fieldVal1 != null && fieldVal2 != null && fieldVal1.equals(fieldVal2)){
            return true;
        }
        return false;
    }

    private JSONObject getLastOrder(){
        JSONObject lastOrder = null;
        if(!orderList.isEmpty()){
            lastOrder = orderList.get(orderList.size() - 1);
        }
        return lastOrder;
    }

    public double getCurrentPallet() {
        return currentPallet;
    }

    public Integer getMaxPallet() {
        return maxPallet;
    }

    public Double getMinStarttime() {
        return minStarttime;
    }

    public List<JSONObject> getOrderList() {
        return orderList;
    }

    public JSONObject getVehicle() {
        return vehicle;
    }
}
